package com.fantastic.service.lwf.impl;

import com.fantastic.common.exception.enums.RespStatusInfo;
import com.fantastic.common.exception.sql.SelectException;
import com.fantastic.common.utils.FilesUtils;
import com.fantastic.common.utils.IsEmptyUtils;
import com.fantastic.mapper.lwf.OrderDetailsInfoMapper;
import com.fantastic.mapper.lwf.OrdersInfoMapper;
import com.fantastic.mapper.lwf.SetMealInfoMapper;
import com.fantastic.mapper.lwf.VarietyInfoMapper;
import com.fantastic.model.pojo.zjs.OrderDetails;
import com.fantastic.model.pojo.zjs.Orders;
import com.fantastic.model.pojo.zjs.SetMeal;
import com.fantastic.model.pojo.zjs.Variety;
import com.fantastic.model.vo.lwf.app.MyOrderFoodsVo;
import com.fantastic.model.vo.lwf.app.MyOrderVo;
import com.fantastic.service.lwf.OrdersInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author .FengYe.
 * @date 2024-06-11
 */
@Service
public class OrdersInfoServiceImpl implements OrdersInfoService {

    @Resource
    private OrdersInfoMapper ordersInfoMapper;

    @Resource
    private OrderDetailsInfoMapper orderDetailsInfoMapper;

    @Resource
    private VarietyInfoMapper varietyInfoMapper;

    @Resource
    private SetMealInfoMapper setMealInfoMapper;

    @Override
    public List<MyOrderVo> queryOrders(int aId, int way) {
        // 初始化
        List<MyOrderVo> myOrderVos = new ArrayList<>();

        // 获取该用户所有订单
        List<Orders> orders = ordersInfoMapper.listOrdersByaId(aId, way);


        for (Orders order : orders) {
            // 初始化菜品信息
            List<MyOrderFoodsVo> myOrderFoodsVos =  new ArrayList<>();
            MyOrderVo myOrderVo = new MyOrderVo();

            // 获取它点单的数量
            Integer num = orderDetailsInfoMapper.orderDetailsNumByoId(order.getOrderId());

            // 存入订单信息
            myOrderVo.setOId(order.getOrderId())
                    .setOPay(order.getOrderPays())
                    .setOWay(order.getOrderWay() == 1 ? "到店取餐" : "超级闪送")
                    .setOStatus(order.getOrderStatus() == 0 ? "已完成" : "配送中")
                    .setONum(num);

            // 获取它的菜品信息
            List<OrderDetails> orderDetails = orderDetailsInfoMapper.getOrderDetailsByoId(order.getOrderId());
            for (OrderDetails orderDetail : orderDetails) {
                // 初始化集合
                if (orderDetail.getVarietyId() != null && orderDetail.getVarietyId() != 0) {
                    // 获取该订单的菜品信息
                    MyOrderFoodsVo myOrderFoodsVo = varietyInfoMapper.getVarietyByvId(orderDetail.getVarietyId());

                    // 图片 路径
                    myOrderFoodsVo.setPicture(FilesUtils.getSlashParse(myOrderFoodsVo.getPicture()));

                    // 添加到集合中
                    myOrderFoodsVos.add(myOrderFoodsVo);
                    continue;
                }

                // 获取该订单的套餐信息
                MyOrderFoodsVo myOrderFoodsVo = setMealInfoMapper.getSetMealBymId(orderDetail.getMealId());

                // 图片 路径
                myOrderFoodsVo.setPicture(FilesUtils.getSlashParse(myOrderFoodsVo.getPicture()));

                // 添加到集合
                myOrderFoodsVos.add(myOrderFoodsVo);
            }

            // 添加到集合中
            myOrderVo.setMyOrderFoodsVos(myOrderFoodsVos);
            myOrderVos.add(myOrderVo);
        }

        IsEmptyUtils.listIsEmpty(new SelectException(RespStatusInfo.SELECT_NULL), myOrderVos);
        return myOrderVos;
    }
}
